﻿using System;
using System.ComponentModel;
using NetBay.Core.BusinessEntities.Enum;
using System.Runtime.Serialization;

namespace NetBay.Core.BusinessEntities
{
    [DataContract]
    public class GeneralParameter : ReturnObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private GeneralParameterDomain _domain;
        private GeneralParameterName _enumName;
        private bool _isEnabled;
        private string _description;
        private string _value;
        private GeneralParameterType _valueType;
        private bool _isMandatory;
        private string _name;
        private long _identifier;

        [DataMember]
        public long Identifier
        {
            get { return _identifier; }
            set
            {
                _identifier = value;
                OnPropertyChanged("Identifier");
            }
        }

        [DataMember]
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        [DataMember]
        public GeneralParameterDomain Domain
        {
            get { return _domain; }
            set
            {
                _domain = value;
                OnPropertyChanged("Domain");
            }
        }

        [DataMember]
        public GeneralParameterName EnumName
        {
            get { return _enumName; }
            set
            {
                _enumName = value;
                OnPropertyChanged("EnumName");
            }
        }

        [DataMember]
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }

        [DataMember]
        public string Value
        {
            get { return _value; }
            set
            {
                _value = value;
                OnPropertyChanged("Value");
            }
        }

        [DataMember]
        public GeneralParameterType ValueType
        {
            get { return _valueType; }
            set
            {
                _valueType = value;
                OnPropertyChanged("ValueType");
            }
        }

        [DataMember]
        public bool IsMandatory
        {
            get { return _isMandatory; }
            set
            {
                _isMandatory = value;
                OnPropertyChanged("IsMandatory");
            }
        }

        [DataMember]
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Determines whether this instance [can parse value].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if this instance [can parse value]; otherwise, <c>false</c>.
        /// </returns>
        public bool CanParseValue()
        {
            return CanParseValue(this.ValueType, this.Value);
        }

        /// <summary>
        /// Determines whether this instance [can parse value] the specified type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="value">The value.</param>
        /// <returns>
        ///   <c>true</c> if this instance [can parse value] the specified type; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanParseValue(GeneralParameterType type, object value)
        {
            switch (type)
            {
                case GeneralParameterType.String:
                    return true;

                case GeneralParameterType.Integer:
                    Int64 IntResult;
                    return Int64.TryParse(value.ToString(), out IntResult);

                case GeneralParameterType.Decimal:
                    Decimal decimalResult;
                    return Decimal.TryParse(value.ToString(), out decimalResult);

                case GeneralParameterType.Boolean:
                    try
                    {
                        if (Convert.ToInt32(value) >= 0 && Convert.ToInt32(value) <= 1)
                        {
                            return true;
                        }
                        return false;
                    }
                    catch
                    {
                        return false;
                    }
            }

            return false;
        }

        public bool MandatoryStatisfy()
        {
            if (IsMandatory)
            {
                return !string.IsNullOrEmpty(Value.ToString());
            }
            return true;
        }

        #region Constructors
        
        public GeneralParameter()
        {
        }

        /// <summary>
        /// Only for error
        /// </summary>
        /// <param name="ErrorMessage"></param>
        public GeneralParameter(string errorMessage, Exception applicationException = null)
            : base(errorMessage, applicationException)
        {
        }


        #endregion
    }
}
